<?php
	include ('../defines.php');

	FunctionsHelper::includeFiles(
		array(
			ADMINISTRATOR_CONTROLLER_DIRECTORY . 'surveys.php',
			ADMINISTRATOR_CONTROLLER_DIRECTORY . 'options.php',
			ADMINISTRATOR_CONTROLLER_DIRECTORY . 'users.php'
		)
	);
	
	$users = new Users();
	if (!$users->isLoggedUser())
		header('Location:' . ADMINISTRATOR_VIEW_PATH . 'login.php');

	$user = $users->getLoggedUser();
	$surveys = new Surveys();
	
	if (!empty($_POST)){
		$arrSurvey = array();

		unset($_POST['sbmSubmit']);

		$arrSurvey['options'] = array();
		foreach ($_POST as $key => $value){
			if (strstr($key, 'option_')){
				$name = explode('option_', $key);
				if (strstr($name[1], 'new_'))
					$option_id = 0;
				else
					$option_id = $name[1];
				
				$arrSurvey['options'][] = array(
					'option_id'		=> $option_id,
					'description' 	=> utf8_encode($value)
				);
			}
			else{
				$arrSurvey[$key] = utf8_encode($value);
			}
		}

		if ($_POST['action'] != 'a'){
			if ($surveys->updateSurvey($arrSurvey))
				header('Location:' . ADMINISTRATOR_VIEW_PATH . 'surveys.php');
		}else if ($_POST['action'] == 'a'){
			if ($surveys->createSurvey($arrSurvey))
				header('Location:' . ADMINISTRATOR_VIEW_PATH . 'surveys.php');	
		}		
	}

	if ($_GET['action'] != 'a'){
		$survey = $surveys->getSurvey(
			array(
				'survey_id' => $_GET['id']
			)
		);
	}else{
		$survey = $surveys->getSurvey();
	}

	FunctionsHelper::includeTop();
?>
<div class="withBorder" id="main">
	<script type="text/javascript">
		$(function(){
			$('#back').click(function(){
				document.location.href='<?=ADMINISTRATOR_VIEW_PATH;?>surveys.php';
			});
			$('.delete').click(function(){
				varUrl = '<?=ADMINISTRATOR_CONTROLLER_PATH; ?>operations.php';
				varData = { 'id' : this.id, 'controller' : 'surveys', 'operation' : 'deleteOption' };
				callAjax(varUrl, 'post', varData, funcSuccess);
				location.reload(true);
			});
			$('.addOption').click(function(){
				input = $(this).parent().find('input');
				value = input.val();
				countOptions = countSelectedOptions();

				textHtml = '<br /><input type="text" name="option_new_' + (countOptions+1) + '" value="' + value + '" id="' + (countOptions+1) + '" class="selectedOption tinyInput">';

				if (countOptions == 0){
					$('.selectedOptions').html(textHtml);
				}else{
					$('#' + countOptions + '.selectedOption').after(textHtml);
				}
				
				input.val('');
			});
		});

		var countSelectedOptions = function(){
			return $('.selectedOptions .selectedOption').length;
		};
	</script>
	<div class="mainABM">
		<div class="ABMtitle"><h1>ADMINISTRADOR DE ENCUESTAS</h1></div>
		<form id='frmSurvey' class='form' method='POST' enctype="multipart/form-data">
			<input type='hidden' name='survey_id' id='survey_id' value='<?= $survey->getSurveyId(); ?>' />
			<input type='hidden' name='action' id='action' value='<?= $_GET['action']; ?>' />
			<div class="label">Encuesta:</div><input type='text' value='<?= utf8_decode($survey->getDescription());?>' class='required tinyInput' name='description' id='description' />
			<div class="label">Opciones:</div>
			<br />
			<br />
			<div id="allOptions">
				<?php 
				foreach ($survey->getOptions() as $Option){
				?>
				<span><?= utf8_decode($Option->getDescription()); ?></span>
				<a href="#" class="delete" id="<?= $Option->getOptionId(); ?>">Eliminar</a>
				<br />
				<?php 
				}
				?>
				<br />
			</div>
			<br />
			<div class="selectedOptions"></div>
			<div class="options">
				<input type="text" name="" value="" id="" class="selectedOption tinyInput">
				<a href="#" class="addOption" id="0">Agregar Opci&oacute;n</a>
			</div>
			<br />
			<input type="checkbox" name='active' id='active' value='1' <? echo $survey->getActive() == 1 ? 'checked = "checked"' : ''; ?> /><label for="active">Activo</label>
			<br />
			<div id="button"><input type="submit" name='sbmSubmit' id='sbmSubmit' value='Aceptar'  /></div>
		</form>
		<img class="back" src='<?= ADMINISTRATOR_IMAGE_PATH . 'back.png'; ?>' id="back" width='64' height='52' />
	</div>
</div>

<?php
	
	FunctionsHelper::includeBottom();
?>